<template>
  <Article :form="form" @handleClick="handleClick" />
</template>

<script>
import Article from '@/views/article/components/article'
import { getArticleById, updateArticle } from '@/api/article'

export default {
  name: 'ArticleEdit',
  components: { Article },
  data() {
    return {
      form: {},
      articleId: null
    }
  },
  created() {
    const articleId = this.$route.params['articleId']
    console.log(articleId)
    if (articleId) {
      this.$store.dispatch('app/changeEditArticleId', articleId)
      this.articleId = articleId
    } else {
      this.articleId = this.$store.getters.editArticleId
    }
    getArticleById(this.articleId).then(res => {
      this.form = res.data
    })
  },
  methods: {
    handleClick(form) {
      this.updateArticle(form)
    },
    updateArticle(form) {
      updateArticle(this.articleId, form).then(() => {
        this.$message({
          message: '发布成功',
          type: 'success'
        })
        this.$router.push({ name: 'ArticleList' })
      })
    }
  }
}
</script>

<style scoped>

</style>
